package yambal.net.google.Blog
{
	import flash.events.Event;
	import com.adobe.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	
	import yambal.net.google.Authentication;
	import yambal.net.google.Requester;
	import yambal.net.google.events.RequesterEvent;
	
	public class Blogger extends Requester
	{
		// http://code.google.com/intl/ja/apis/blogger/docs/2.0/developers_guide_protocol.html
		
		public static const BLOGGER_EP:String = "http://www.blogger.com/feeds";
		
		public var userID:String;
		public var userNicname:String;
		public var profileUrl:String;
		public var updated:Date;
		public var iconUrl:String;
		
		[Bindable]
		public var Blogs:ArrayCollection
		
		public function Blogger()
		{
		}
		
		/**
		 * Authenticate
		 * 	もし、認証が必要な操作を行う場合は
		 *	setAuthenticateを行います。
		 **/
		public function setAuthenticate(email:String, password:String, isHosted:Boolean = false):void{
			trace("setAuthenticate");
			var auth:Authentication = new Authentication();

			auth.addEventListener(Event.COMPLETE, this.onAuthenticate, isHosted); 
			auth.authenticate(email, password, 'blogger');
		}
		
		private function onAuthenticate(e:Event):void{
			var auth:Authentication = e.currentTarget as Authentication;
			auth.removeEventListener(Event.COMPLETE, this.onAuthenticate);
			
			this.Auth = auth;
			this.getBlogList();
		}
		
		/**
		 * getBlogList
		 **/
		public function getBlogList(userID:String = null):void{
			trace("getBlogList");
			this.Blogs = null;
			
			if(userID != null && userID != this.userID){
				// もし、対象のユーザが指定されているならば
				//trace("target-user:" + PICASA_EP + userID);

			}else if(this.Auth != null && this.Auth.is_ok && this.Auth.service == 'blogger'){
				// もし、認証済ならばそのユーザを使う
				this.addEventListener(RequesterEvent.RESPONSE, this.onResponseGetBlog);
				this.request(BLOGGER_EP + "/default/blogs");
			}else{
				trace('false');
			}
		}
		
		private function onResponseGetBlog(e:RequesterEvent):void{
			this.removeEventListener(RequesterEvent.RESPONSE,this.onResponseGetBlog);
			var res:XML = e.result;

			var atom:Namespace = res.namespace();
			var openSearch:Namespace = res.namespace('openSearch');
			var gphoto:Namespace = res.namespace('gphoto');

			this.userNicname = res.atom::author.atom::name;
			this.profileUrl = res.atom::author.atom::uri;
			this.userID = this.profileToUserId(this.profileUrl);

			//this.updated = DateUtil.parseW3CDTF(res.atom::updated);
			this.iconUrl = res.atom::icon;
			
			this.Blogs = new ArrayCollection();
			for each (var entry:XML in res.atom::entry) {
				var blog:Blog = this.parseToBlog(entry);
				blog.requestPost(3);
				trace(blog);
				this.Blogs.addItem(blog);
			}
		}
		
		/**
		 * プロフィールURLからユーザのIDを推察する
		 **/
		private function profileToUserId(url:String):String{
			var s:Array = url.split("/");
			var id:String = s[s.length - 1];
			return id;
		}
		
		// -----------------------------------------------------------------------------------------
		/**
		 * EntryをAlbumにパースする
		 **/
		private function parseToBlog(entry:XML):Blog{
			var atom:Namespace = entry.namespace();
			var gphoto:Namespace = entry.namespace('gphoto');
			
			var blog:Blog = new Blog();
			blog.fullId = entry.atom::id;
			blog.published = DateUtil.parseW3CDTF(entry.atom::published);
			blog.updated = DateUtil.parseW3CDTF(entry.atom::updated);
			blog.title = entry.atom::title;
			blog.summary = entry.atom::summary;
			blog.author = new Author();
			blog.author.name = entry.atom::author.atom::name;
			blog.author.profileUrl = entry.atom::author.atom::uri;
			blog.author.userID = this.profileToUserId(blog.author.profileUrl);
			
			// カテゴリ
			blog.categories = new ArrayCollection();
			var categories:XMLList = entry.atom::category;
			for each (var category:XML in categories) {
				blog.categories.addItem(String(category.@term));
			}
			
			// Link
			var links:XMLList = entry.atom::link;
			for each (var link:XML in links) {
				if(link.@rel == "http://schemas.google.com/g/2005#post"){
					blog.postUrl = link.@href;
					
				}else if(link.@rel == "http://schemas.google.com/g/2005#feed"){
					blog.feedUrl = link.@href;

				}else if(link.@rel == "self"){
					blog.selfUrl = link.@href;

				}else if(link.@rel == "alternate"){
					blog.alternateUrl = link.@href;
					
				}else if(link.@rel == "http://schemas.google.com/blogger/2008#template"){
					blog.templateUrl = link.@href;

				}else if(link.@rel == "http://schemas.google.com/blogger/2008#settings"){
					blog.settingsUrl = link.@href;
				}
			}

			return blog;
		}
	}
}